#!/usr/bin/python3

# -*- encoding:utf-8 -*-

#安装方式
# git clone https://github.com/KscSDK/ksc-sdk-python.git
# cd ksc-sdk-python && python3 setup.py install

import json,pprint
import sys

from prettyprinter import  prettyprinter
from kscore.session import get_session

if __name__ == "__main__":
    s = get_session()
    region='cn-beijing-6'   #设置区域
    slbClient = s.create_client("slb", region, use_ssl=True)
    # LoadBalancerId = 'a43e2900-1419-4493-adc3-83c8bbebf322' #负载均衡器id
    LoadBalancerId = sys.argv[1]
    ListenerState = 'start' #监听器的状态,start|stop
    ListenerProtocol = 'TCP' #监听器协议,TCP\UDP（香港地区暂不支持 UDP 协议）\HTTP\HTTPS
    Method = 'RoundRobin' #监听器的转发方式,RoundRobin | LeastConnections | MasterSlave
    SessionState = 'stop' #会话保持的状态,可取值: start |stop
    host_list = []
    with open('host_list.txt', 'r', encoding='utf-8') as f:
        for i in f.readlines():
            host_list.append(eval(i))
    print(host_list)
    # host_list = [
    #     {"ListenerName": 'test100', 'ListenerPort': 100, 'RealServerIp': '172.16.7.135', 'RealServerPort': 82,
    #      'Weight': 1},
    #     {"ListenerName": 'test101', 'ListenerPort': 101, 'RealServerIp': '172.16.7.135', 'RealServerPort': 82,
    #      'Weight': 1},
    #     {"ListenerName": 'test102', 'ListenerPort': 102, 'RealServerIp': '172.16.7.135', 'RealServerPort': 82,
    #      'Weight': 1},
    # ]
    #ListenerName 监听器对名称
    #ListenerPort 监听器的端口
    #RealServerIp 后端主机ip
    #RealServerPort 后端主机ip端口
    #Weight 权重
    for i in host_list:
        listeners_par={
            'LoadBalancerId':LoadBalancerId,
            'ListenerState':ListenerState,
            'ListenerName':i["ListenerName"],
            'ListenerProtocol':ListenerProtocol,
            'ListenerPort':i["ListenerPort"],
            'Method':Method,
            'SessionState':SessionState
        }
        listeners_status =slbClient.create_listeners(**listeners_par)
        print("+++++++++++++++监听器创建成功++++++++++++++++++++++")
        print(listeners_status)
        ListenerId = listeners_status["ListenerId"]
        host_par2 = {
            'RealServerType':'host',
            'ListenerId':ListenerId,
            'RealServerIp':i["RealServerIp"],
            'RealServerPort':i["RealServerPort"],
            'Weight':i["Weight"]
        }
        host_result = slbClient.register_instances_with_listener(**host_par2)
        print("+++++++++++++++监听器绑定后端服务器++++++++++++++++++++++")
        print(host_result)
        #HealthCheckState 健康检查保持的状态,可取值: start |stop
        #HealthyThreshold 健康阈值,可取值: 1-10，单位：次
        #Interval 健康检查时间间隔,可取值: 1-1000，单位：秒
        #Timeout 健康检查超时时间,可取值: 1-3600，单位：秒
        #UnhealthyThreshold 不健康阈值,可取值: 1-10，单位：次

        host_check_par = {
            'ListenerId':ListenerId,
            'HealthyThreshold':5,
            'Interval':5,
            'UnhealthyThreshold':5,
            'Timeout':4,
            'HealthCheckState':'start'
        }
        check_result = slbClient.configure_health_check(**host_check_par)
        print("+++++++++++++++健康检查开启++++++++++++++++++++++")
        print(check_result)





